package be.castanea.client.input;

import be.castanea.client.model.Movement;
import be.castanea.client.model.Self;
import com.jme.input.ChaseCamera;
import com.jme.input.MouseInput;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.system.DisplaySystem;

/**
 * Castanea
 * 2009
 * @author Geert van Leemputten, Steven Rymenans, Bart Van Hooydonck
 */
public class CameraHandler extends ChaseCamera {

    private int mouseX;
    private int mouseY;

    public CameraHandler(Self self) {
        super(DisplaySystem.getDisplaySystem().getRenderer().getCamera(), self);
        setEnableSpring(false);
        setStayBehindTarget(true);
        setMaintainAzimuth(true);
        setForceAzimuthUpdate(true);
        setMaxDistance(20);
        setMinDistance(2);
        getMouseLook().setEnabled(false);
        getMouseLook().setSpeed(0.2f);
        getMouseLook().setInvertedY(true);
        getMouseLook().setMouseXMultiplier(1f);
        getMouseLook().setMouseYMultiplier(1f);
        getMouseLook().setMouseRollMultiplier(2f);
        getMouseLook().setMaxRollOut(getMaxDistance());
        getMouseLook().setMinRollOut(getMinDistance());
        getMouseLook().setMaxAscent(90);
        getMouseLook().setMinAscent(-90);
    }  

    @Override
    public void update(float time) {
        super.update(time);
        // button 0 = LEFT
        boolean left = MouseInput.get().isButtonDown(0);
        // button 1 = RIGHT
        boolean right = MouseInput.get().isButtonDown(1);
        // mouselook
        if (left || right) {
            if (!getMouseLook().isEnabled()) {
                getMouseLook().setEnabled(true);
                mouseX = MouseInput.get().getXAbsolute();
                mouseY = MouseInput.get().getYAbsolute();
                MouseInput.get().setCursorVisible(false);
            }
        } else {
            if (getMouseLook().isEnabled()) {
                getMouseLook().setEnabled(false);
                MouseInput.get().setCursorVisible(true);
                MouseInput.get().setCursorPosition(mouseX, mouseY);
            }
        }
        // rotation
        if (right) {
            // we bereken de snelheid om ons personage in de dezelfde richting te plaatsen als de camera
            Quaternion camRot = getRotation();
            float camAngle = camRot.toAngleAxis(Vector3f.UNIT_Z);
            Quaternion chaRot = Self.getInstance().getWorldRotation();
            float chaAngle = chaRot.toAngleAxis(Vector3f.UNIT_Z);
            float angle = (camAngle - chaAngle) / time;
            int turnSpeed = Math.round(angle);

            Movement movement = Self.getInstance().getMovement();
            movement = new Movement(movement.getForwardSpeed(), movement.getSidewaySpeed(), turnSpeed);
            Self.getInstance().setMovement(movement);
        }
    }

    private Quaternion getRotation() {
        // Get the camera direction as a Quaternion.
        Quaternion cameraDir = new Quaternion();
        Vector3f[] axis = new Vector3f[3];
        axis[0] = getCamera().getLeft();
        axis[1] = getCamera().getUp();
        axis[2] = getCamera().getDirection();
        cameraDir.fromAxes(axis);
        return cameraDir;
    }
}
